import { formatMoney } from '../../../../utils/util' // 格式化金额

/**
 * @see 该页面是通用订单详情界面，此界面从不同的业务进入，需要带入参数进入。进入时根据业务类型来做不同的处理。
 * @see 接受参数主要有：
 * @param {*type 业务类型（电子成绩单、电子证明）} String
 * @example /pages/workService/components/fileAndReportCardRecord/index?type=电子证明
 */
Page({
  data: {
    orderId: '', // 订单id
    isLoading: false,
    showModal: false,
    email: '',
    recordDetails: [] // 订单详情数据
  },
  onLoad (option) {
    const { orderId } = option
    this.setData({
      orderId
    })
    this.getOrderDetails()
  },
  /**
   * @see 接口--订单详情
   */
  getOrderDetails () {
    const { orderId } = this.data
    const request = { orderId }
    wx.showLoading({
      title: '加载中请稍后...',
      mask: true
    })
    wx.$API['workService/getOrderDetails'](request).then((result) => {
      wx.hideLoading()
      wx.stopPullDownRefresh()
      const { data } = result
      data.forEach((item) => {
        switch (item.tradeStatus) {
          case 1:
            item.tradeStatusText = '交易成功'
            item.tradeStatusColor = '#31AD37'
            break
          case 2:
            item.tradeStatusText = '交易失败'
            item.tradeStatusColor = '#F34E0D'
            break
          case 3:
            item.tradeStatusText = '处理中'
            item.tradeStatusColor = '#FFB911'
            break
          default:
            item.tradeStatusText = '无'
            break
        }
        switch (item.payWay) {
          case 1:
            item.payWayText = 'U米支付'
            item.payCountPriceWay = 'U米'
            break
          case 2:
            item.payWayText = '微信支付'
            item.payCountPrice = formatMoney(item.payCountPrice)
            item.payCountPriceWay = '元'
            break
          case 3:
            item.payWayText = '扫码支付'
            item.payCountPrice = formatMoney(item.payCountPrice)
            item.payCountPriceWay = '元'
            break
          case 4:
            item.payWayText = '免费次数'
            item.payCountPrice = ''
            item.payCountPriceWay = '无'
            break
          default:
            item.payWayText = '无'
            item.payCountPriceWay = '无'
            break
        }
        // if (item.payWay === 2 && item.getStatus === 2) {
        //   // 能领取U米
        //   item.bIsGetUmi = ''
        //   item.bIsClickGetUmi = 'auto'
        // } else {
        //   // 不能领取U米
        //   item.bIsGetUmi = 'none'
        //   item.bIsClickGetUmi = 'none'
        // }
        item.bIsGetUmi = 'none'
        item.bIsClickGetUmi = 'none'
        if (item.sendMailNum > 0) {
          // 能发送邮件
          item.bIsAgainSendEmail = ''
          item.bIsClickAgainSendEmail = 'auto'
        } else {
          // 不能发送邮件
          item.bIsAgainSendEmail = 'none'
          item.bIsClickAgainSendEmail = 'none'
        }
        this.setData({
          recordDetails: data
        })
      })
    }).catch((err) => {
      wx.stopPullDownRefresh()
      wx.hideLoading()
      wx.showToast({
        title: err,
        icon: 'none'
      })
    })
  },
  /**
   * @see 接口--领取U米（业务：如果支付方式是微信支付，即可领取）
   */
  getUmi () {
    const { orderId } = this.data
    const request = { orderId }
    this.setData({ isLoading: true })
    wx.showLoading({
      title: '加载中请稍后...',
      mask: true
    })
    wx.$API['workService/getUmi'](request).then(() => {
      wx.hideLoading()
      wx.showToast({
        title: '领取成功'
      })
      this.getOrderDetails()
      setTimeout(() => {
        this.setData({ isLoading: false })
      }, 3000)
    }).catch((err) => {
      setTimeout(() => {
        this.setData({ isLoading: false })
      }, 3000)
      wx.hideLoading()
      wx.showToast({
        title: err,
        icon: 'none'
      })
    })
  },
  /**
   * @see 接口--重新发送邮件（业务：如果上链状态||发送邮件状态为失败，即可发送）
   */
  reSendEmail () {
    const { orderId, email } = this.data
    const request = { orderId, toEmail: email }
    this.setData({ isLoading: true })
    wx.showLoading({
      title: '加载中请稍后...',
      mask: true
    })
    wx.$API['workService/reSendEmail'](request).then(() => {
      wx.hideLoading()
      wx.showToast({
        title: '操作成功'
      })
      this.getOrderDetails()
      setTimeout(() => {
        this.setData({ isLoading: false })
      }, 3000)
    }).catch((err) => {
      setTimeout(() => {
        this.setData({ isLoading: false })
      }, 3000)
      wx.hideLoading()
      wx.showToast({
        title: err,
        icon: 'none'
      })
    })
  },
  /**
   * @see 点击领取U米
   */
  handleUmi () {
    if (this.data.isLoading) return
    this.getUmi()
  },
  /**
   * @see 重新发送邮件
   */
  handleAgainSendEmail () {
    if (this.data.isLoading) return
    this.setData({ showModal: true })
  },
  handleinput (event) {
    const email = event.detail.value
    this.setData({ email })
  },
  handleAffirm () {
    const reg = new RegExp(/^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/)
    const email = this.data.email
    if (reg.test(email)) {
      this.setData({ showModal: false })
      this.reSendEmail()
    } else {
      wx.showToast({
        title: '邮箱不正确',
        icon: 'none'
      })
    }
  },
  handleClose () {
    this.setData({ showModal: false })
  },
  /**
   * @see 下拉刷新
   */
  onPullDownRefresh () {
    this.getOrderDetails()
  }
})